/**
 * 上传附件
 * @param obj 当前对象
 * @param callBack 回调函数(当前对象，返回附件地址列表)
 */
function selectAttachment(obj,callBack) {
    var title = $(obj).data('title');
    var url = $(obj).data('url');
    layer.open({
        title: title,
        type: 2,
        area: ['800px', '560px'],
        content: url,
        btn: ['确认', '取消'],
        yes: function(index,layero){
            var selectAttachment = layer.getChildFrame('#select-attachment', index);
            console.info(selectAttachment);
            callBack(obj,selectAttachment.text());
            layer.close(index);
        }
    });
}
/**********************************单附件选择********************************************/
function selectSingleFile(obj,data){
    if(data==''){
        return false;
    }
    var fileinfo = data.split("###");
    var field=$(obj).data('input');
    var html = '<span>';
    html += '<a href="'+fileinfo[0]+'" target="_blank">'+fileinfo[1]+'</a>';
    html += '<a href="javascript:;" onclick="delSingleFile(this,\''+field+'\')" style="margin-left:10px;">删除</a>';
    html += '</span>';
    $("input[name='"+field+"']").val(data);
    $(obj).closest('.selectSingleFile').append(html);
    $(obj).hide();
}
//删除单附件
function delSingleFile(obj,field){
    $("input[name='"+field+"']").val('');
    $(obj).closest('.selectSingleFile').find('.uploader-file-btn').show();
    $(obj).parent().remove();
}
/**********************************单图选择********************************************/
function selectSingleJpg(obj,data){
    if(data==''){
        return false;
    }
    var field=$(obj).data('input');
    var imgHtml = '<span class="uploader-file uploader_status" style="background-image:url('+data+')">'
    imgHtml += '<a href="javascript:;" onclick="delSingleJpg(this,\''+field+'\')" class="imgBar">删除</a>';
    imgHtml += '</span>';
    $("input[name='"+field+"']").val(data);
    $(obj).closest('.selectSingleJpg').append(imgHtml);
    $(obj).hide();
}
//删除单图
function delSingleJpg(obj,field){
    $("input[name='"+field+"']").val('');
    $(obj).closest('.selectSingleJpg').find('.uploader-btn').show();
    $(obj).parent().remove();
}
/**********************************多图选择********************************************/
function selectMoreJpg(obj, data) {
    if (data == '') {
        return false;
    }
    var field=$(obj).data('input');
    $imgs = data.split('|');
    for (i = 0; i < $imgs.length; i++) {
        var html = '<div class="uploader-file uploader_status"';
        html += 'style="background-image:url(\''+$imgs[i]+'\')" data-img="'+$imgs[i]+'">';
        html += '<div class="imgBar btn-group btn-group-justified">';
        html += '<a href="javascript:;" onclick="arrowLeftMoreJpg(this,\''+field+'\')"><i class="fa fa-arrow-left"></i></a>';
        html += '<a href="javascript:;" onclick="arrowRightMoreJpg(this,\''+field+'\')"><i class="fa fa-arrow-right"></i></a>';
        html += '<a href="javascript:;" onclick="delMoreJpg(this,\''+field+'\')"><i class="fa fa-trash"></i></a>';
        html += '</div>';
        html += '</div>';
        $(obj).parent().find('.img-block').append(html);
    }
    $("input[name='"+field+"']").val(getMoreJpg(field));
}
//获取全部选择图片
function getMoreJpg(field) {
    var obj = field+'moreImg';
    data=[];
    $('#'+obj).find('.uploader-file').each(function(){
        data.push($(this).data('img'));
    });
    return data.join('|');
}
//删除多图
function delMoreJpg(obj,field){
    $(obj).closest('.uploader-file').remove();
    $("input[name='"+field+"']").val(getMoreJpg(field));
}
//左移
function arrowLeftMoreJpg(obj,field) {
    var objParentUpload = $(obj).closest('.uploader-file');
    var prevUpload = objParentUpload.prev();
    if(prevUpload.length>0){
        prevUpload.insertAfter(objParentUpload);
    }
    $("input[name='"+field+"']").val(getMoreJpg(field));
}
//右移
function arrowRightMoreJpg(obj,field) {
    var objParentUpload = $(obj).closest('.uploader-file');
    var nextUpload = objParentUpload.next();
    if(nextUpload.length>0){
        nextUpload.insertBefore(objParentUpload);
    }
    $("input[name='"+field+"']").val(getMoreJpg(field));
}
/**********************************多图带表单框选择********************************************/
//选择图片
function selectMoreTextImg(obj, data) {
    if (data == '') {
        return false;
    }
    var tips = $(obj).data('tips');
    var field=$(obj).data('input');
    var $imgs = data.split('|');
    for (i = 0; i < $imgs.length; i++) {
        var html = '<div class="col-sm-6"><div class="uploader-file-img">';
        html += ' <div class="image" style="background-image:url('+$imgs[i]+')" data-img="'+$imgs[i]+'">';
        html += '<div class="imgBar">';
        html += '<a href="javascript:;" onclick="arrowLeftMoreTextJpg(this,\''+field+'\')"><i class="fa fa-arrow-left"></i></a>';
        html += '<a href="javascript:;" onclick="arrowRightMoreTextJpg(this,\''+field+'\')"><i class="fa fa-arrow-right"></i></a>';
        html += '<a href="javascript:;" onclick="delMoreTextJpg(this,\''+field+'\')"><i class="fa fa-trash"></i></a>';
        html += '</div>';
        html += '</div>';
        html += '<div class="images-info"><div class="info"><div class="form-group">';
        html += '<input type="text" onblur="getMoreTextJpg(\''+field+'\');" class="form-control img-title" placeholder="'+tips+'">';
        html += '</div>';
        html += '<div class="form-group">';
        html += '<textarea onblur="getMoreTextJpg(\''+field+'\');" class="form-control img-info" rows="4" placeholder="请输入图片描述..."></textarea>';
        html += '</div>';
        html += '</div>';
        html += '</div>';
        html += '</div>';
        html += '</div>';
        $(obj).parent().find('.img-block').append(html);
    }
    getMoreTextJpg(field);
}
//获取全部选择图片
function getMoreTextJpg(field) {
    var obj = field+'moreTextImg';
    var img=[];
    var imgTitle=[];
    var imgInfo=[];
    $('#'+obj).find('.image').each(function(){
        img.push($(this).data('img'));
    });
    $('#'+obj).find('.img-title').each(function(){
        imgTitle.push($(this).val());
    });
    $('#'+obj).find('.img-info').each(function(){
        imgInfo.push($(this).val());
    });
    var data = img.join('|')+'###'+imgTitle.join('|')+'###'+imgInfo.join('|');
    $("input[name='"+field+"']").val(data);
}
//左移
function arrowLeftMoreTextJpg(obj,field) {
    var objParentUpload = $(obj).closest('.col-sm-6');
    var prevUpload = objParentUpload.prev();
    if(prevUpload.length>0){
        prevUpload.insertAfter(objParentUpload);
    }
    getMoreTextJpg(field);
}
//右移
function arrowRightMoreTextJpg(obj,field) {
    var objParentUpload = $(obj).closest('.col-sm-6');
    var nextUpload = objParentUpload.next();
    if(nextUpload.length>0){
        nextUpload.insertBefore(objParentUpload);
    }
    getMoreTextJpg(field);
}
//删除多图
function delMoreTextJpg(obj,field){
    $(obj).closest('.col-sm-6').remove();
    getMoreTextJpg(field);
}